package com.songcha.module_main.ui.fragment.achievement

import android.os.Bundle
import androidx.compose.runtime.Composable
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.runtime.getValue
import androidx.compose.foundation.border
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.songcha.library_base.compose.BaseComposeMvvmFragment
import com.songcha.library_common.ui.view.compose.ComposeAsyncImage
import com.songcha.library_common.ui.view.compose.ComposeImage
import com.songcha.library_common.util.TimeUtil
import com.songcha.module_memo.bean.wooden_fish.QmyMedalBean
import com.songcha.module_main.R
import com.songcha.module_main.event.RefreshGongDeEvent
import com.songcha.module_main.ui.fragment.rank.RankFragment
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

class AchievementFragment:BaseComposeMvvmFragment<AchievementViewModel>() {

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun getLoadBackgroundColor(): Int {
        return android.graphics.Color.TRANSPARENT
    }

    override fun onGetData() {
        viewModel.getMyQmyMedalList()
    }

    @Composable
    override fun ComposeRootView() {
        ShareView()
    }

    @Composable
    private fun ShareView(){
        val medalBean by viewModel.medalBean.collectAsState()
        if(medalBean == null) return

        var brightMedalCount = 0

        for(item in medalBean!!.list){
            if(item.isBright)
                brightMedalCount ++
        }

        Column(
            modifier = Modifier.fillMaxWidth()
                .verticalScroll(rememberScrollState()),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            //header
            ComposeImage(
                modifier = Modifier
                    .padding(top = 35.dp)
                    .size(205.dp,51.dp),
                id = R.mipmap.main_bg_qmy_achievement_title)

            Row(
                modifier = Modifier.padding(top = 22.dp),
                verticalAlignment = Alignment.CenterVertically
            ){
                Text(text = "已获得", fontSize = 16.sp,color = Color(0xffFFCCA9))
                Text(text = brightMedalCount.toString(), fontSize = 18.sp,color = Color(0xffFF8A3C))
                Text(text = "枚勋章，超过", fontSize = 16.sp,color = Color(0xffFFCCA9))
                Text(text = medalBean!!.surpassPercentage.toString() + "%", fontSize = 16.sp,color = Color(0xffFF8A3C))
                Text(text = "的用户", fontSize = 16.sp,color = Color(0xffFFCCA9))
            }

            val medalList = medalBean!!.list
            val lineCount = 3

            if(medalList.isEmpty()) return

            Box(
                modifier = Modifier.padding(14.dp,38.dp,14.dp,10.dp)
                    .fillMaxWidth()
                    .border(width = 0.5.dp,color = Color(0xff907664),shape = RoundedCornerShape(5.dp))
                    .background(Color(0xff22201F))
            ){
                Column(
                    modifier = Modifier.fillMaxWidth()
                ){
                    for(i in 0 until kotlin.math.ceil(medalList.size / lineCount * 1.0).toInt()) {
                        Row(
                            modifier = Modifier.padding(top = if(i == 0) 16.dp else 0.dp)
                                .fillMaxWidth()
                        ){
                            for (j in 0 until lineCount) {
                                if(i * lineCount + j <= medalList.size- 1){
                                    val item = medalList[i * lineCount + j]
                                    MedalItemView(Modifier.weight(1f),i * lineCount + j,item)
                                }
                            }
                        }

                    }
                }

            }

            /*LazyVerticalGrid(
                modifier = Modifier
                    .fillMaxWidth(),
                columns = GridCells.Fixed(3)
            ){

                itemsIndexed(list){index, item ->
                    MedalItemView(index,item)
                }

            }*/
        }
    }

    @Composable
    private fun MedalItemView(modifier:Modifier,index:Int,item: QmyMedalBean.DataBean.MedalBean){
        Column(modifier = Modifier
            .padding(bottom = 10.dp)
            .then(modifier),
            horizontalAlignment = Alignment.CenterHorizontally) {

            ComposeAsyncImage(
                modifier = Modifier.size(86.dp),
                url = if(item.isBright) item.logoBright else item.logoAsh
            )

            Text(
                modifier = Modifier.padding(top = 8.dp),
                text = item.headline, fontSize = 14.sp,color = Color(0xffFFCCA9))

            val desc = when(item.type){
                1 -> "完成${item.myCondition}次连击"
                2 -> "累计获取${item.myCondition}功德"
                3 -> "累计敲木鱼${item.myCondition}天"
                4 -> "进入日榜前${item.myCondition}"
                else -> ""
            }
            Text(
                modifier = Modifier.padding(top = 10.dp),
                text = desc, fontSize = 11.sp,color = Color(0xffFFCCA9))

            var timeStr = ""
            if(item.createTime != null && item.createTime.isNotEmpty()){
                val ts = TimeUtil.dateString2Stamp(item.createTime)
                timeStr = TimeUtil.formatTime(ts,"yyyy/MM/dd") + "获得"
            }
            Text(
                modifier = Modifier.padding(top = 5.dp),
                text = timeStr, fontSize = 10.sp,color = Color(0xffFFCCA9))
        }
    }

    @Subscribe
    fun onRefreshGongDe(event: RefreshGongDeEvent){
        val frag = (requireParentFragment() as RankFragment)
        if(!frag.isCurrentBottomTab()) return
        onGetData()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}